package o;

import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import o.fuo.d;
import o.fvf.a;

public final class fve implements fuo {
    final fvj ˏ;

    public fve(fvj o_fvj) {
        this.ˏ = o_fvj;
    }

    public fvb ˎ(d dVar) throws IOException {
        fvb ˊ;
        fvb o_fvb = null;
        if (this.ˏ != null) {
            ˊ = this.ˏ.ˊ(dVar.ˊ());
        } else {
            ˊ = o_fvb;
        }
        fvf ˊ2 = new a(System.currentTimeMillis(), dVar.ˊ(), ˊ).ˊ();
        fus o_fus = ˊ2.ˋ;
        fvb o_fvb2 = ˊ2.ˊ;
        if (this.ˏ != null) {
            this.ˏ.ˋ(ˊ2);
        }
        if (ˊ != null && o_fvb2 == null) {
            fvg.ॱ(ˊ.ᐝ());
        }
        if (o_fus == null && o_fvb2 == null) {
            return new fvb.d().ˎ(dVar.ˊ()).ॱ(fut.HTTP_1_1).ॱ(504).ˊ("Unsatisfiable Request (only-if-cached)").ॱ(fvg.ॱ).ॱ(-1).ˏ(System.currentTimeMillis()).ˏ();
        }
        if (o_fus == null) {
            return o_fvb2.ʽ().ˋ(ˎ(o_fvb2)).ˏ();
        }
        try {
            o_fvb = dVar.ॱ(o_fus);
            if (o_fvb2 != null) {
                if (o_fvb == null || o_fvb.ˊ() != 304) {
                    fvg.ॱ(o_fvb2.ᐝ());
                } else {
                    ˊ = o_fvb2.ʽ().ॱ(ˏ(o_fvb2.ʼ(), o_fvb.ʼ())).ॱ(o_fvb.ॱˊ()).ˏ(o_fvb.ˏॱ()).ˋ(ˎ(o_fvb2)).ˏ(ˎ(o_fvb)).ˏ();
                    o_fvb.ᐝ().close();
                    this.ˏ.ˊ();
                    this.ˏ.ˎ(o_fvb2, ˊ);
                    return ˊ;
                }
            }
            if (o_fvb == null) {
                throw new IOException("networkResponse is null in cache Interceptor");
            }
            ˊ = o_fvb.ʽ().ˋ(ˎ(o_fvb2)).ˏ(ˎ(o_fvb)).ˏ();
            if (this.ˏ == null) {
                return ˊ;
            }
            if (fvr.ˊ(ˊ) && fvf.ˎ(ˊ, o_fus)) {
                return ˋ(this.ˏ.ˏ(ˊ), ˊ);
            }
            if (!fvt.ˎ(o_fus.ˋ())) {
                return ˊ;
            }
            try {
                this.ˏ.ˏ(o_fus);
                return ˊ;
            } catch (IOException e) {
                return ˊ;
            }
        } finally {
            if (o_fvb == null && ˊ != null) {
                fvg.ॱ(ˊ.ᐝ());
            }
        }
    }

    private static fvb ˎ(fvb o_fvb) {
        if (o_fvb == null || o_fvb.ᐝ() == null) {
            return o_fvb;
        }
        return o_fvb.ʽ().ॱ(null).ˏ();
    }

    private fvb ˋ(final fvd o_fvd, fvb o_fvb) throws IOException {
        if (o_fvd == null) {
            return o_fvb;
        }
        fxx ॱ = o_fvd.ॱ();
        if (ॱ == null) {
            return o_fvb;
        }
        final fxg ˋ = o_fvb.ᐝ().ˋ();
        final fxj ˋ2 = fxr.ˋ(ॱ);
        fxw anonymousClass2 = new fxw(this) {
            boolean ˊ;
            final /* synthetic */ fve ˎ;

            public long ˊ(fxi o_fxi, long j) throws IOException {
                try {
                    long ˊ = ˋ.ˊ(o_fxi, j);
                    if (ˊ == -1) {
                        if (!this.ˊ) {
                            this.ˊ = true;
                            ˋ2.close();
                        }
                        return -1;
                    }
                    o_fxi.ˊ(ˋ2.ˏ(), o_fxi.ॱ() - ˊ, ˊ);
                    ˋ2.ˈ();
                    return ˊ;
                } catch (IOException e) {
                    if (!this.ˊ) {
                        this.ˊ = true;
                        o_fvd.ˏ();
                    }
                    throw e;
                }
            }

            public fxv ˊ() {
                return ˋ.ˊ();
            }

            public void close() throws IOException {
                if (!(this.ˊ || fvg.ˋ((fxw) this, 100, TimeUnit.MILLISECONDS))) {
                    this.ˊ = true;
                    o_fvd.ˏ();
                }
                ˋ.close();
            }
        };
        return o_fvb.ʽ().ॱ(new fvx(o_fvb.ˋ(HttpContants.KEY_CONTENT_TYPE), o_fvb.ᐝ().ˏ(), fxr.ˎ(anonymousClass2))).ˏ();
    }

    private static fui ˏ(fui o_fui, fui o_fui2) {
        int i;
        int i2 = 0;
        fui.a aVar = new fui.a();
        int ˋ = o_fui.ˋ();
        for (i = 0; i < ˋ; i++) {
            String ॱ = o_fui.ॱ(i);
            String ˋ2 = o_fui.ˋ(i);
            if (!("Warning".equalsIgnoreCase(ॱ) && ˋ2.startsWith("1")) && (ˋ(ॱ) || !ॱ(ॱ) || o_fui2.ॱ(ॱ) == null)) {
                fux.ˏ.ˋ(aVar, ॱ, ˋ2);
            }
        }
        i = o_fui2.ˋ();
        while (i2 < i) {
            String ॱ2 = o_fui2.ॱ(i2);
            if (!ˋ(ॱ2) && ॱ(ॱ2)) {
                fux.ˏ.ˋ(aVar, ॱ2, o_fui2.ˋ(i2));
            }
            i2++;
        }
        return aVar.ˊ();
    }

    static boolean ॱ(String str) {
        if ("Connection".equalsIgnoreCase(str) || "Keep-Alive".equalsIgnoreCase(str) || "Proxy-Authenticate".equalsIgnoreCase(str) || "Proxy-Authorization".equalsIgnoreCase(str) || "TE".equalsIgnoreCase(str) || "Trailers".equalsIgnoreCase(str) || "Transfer-Encoding".equalsIgnoreCase(str) || "Upgrade".equalsIgnoreCase(str)) {
            return false;
        }
        return true;
    }

    static boolean ˋ(String str) {
        return "Content-Length".equalsIgnoreCase(str) || "Content-Encoding".equalsIgnoreCase(str) || HttpContants.KEY_CONTENT_TYPE.equalsIgnoreCase(str);
    }
}
